<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){
	
	case "base":
	
		//用户是否登录
		$userid = aac('user')->isLogin();

		$studyid = intval($_GET['studyid']);

		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));

		if($strStudy['userid'] == $userid || $TS_USER['isadmin']==1){
			
			$strStudy['onecate'] = $new['study']->find('study_cates',array(
				'cateid'=>$strStudy['cateid'],
			));
			
			$strStudy['twocate'] = $new['study']->find('study_cates',array(
				'cateid'=>$strStudy['cateid2'],
			));
			
			//课程分类
			$arrCates = $new['study']->findAll('study_cates',array('recateid'=>0),'cateid asc');
		
			
			$title = '编辑课程基本信息';
			include template("edit_base");
			
		}else{
		
			tsNotice('非法操作！');
		
		}
		
		break;
		
	case "basedo":
	
		//用户是否登录
		$userid = aac('user')->isLogin();

		$studyid = intval($_POST['studyid']);

		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));

		if($strStudy['userid'] == $userid || $TS_USER['isadmin']==1){
	
			
			$studyname = trim($_POST['studyname']);
			$studydesc = tsCleanContent($_POST['studydesc']);
			
			$cateid = intval($_POST['cateid']);
			$cateid2 = intval($_POST['cateid2']);
			
			$price = floatval($_POST['price']);
			
			if($cateid==0 && $strStudy['cateid']){
				$cateid = $strStudy['cateid'];
			}
			
			if($cateid2==0 && $strStudy['cateid']){
				$cateid2 = $strStudy['cateid2'];
			}
			
			if($studyname=='' || $studydesc == '' || $cateid == 0) {
				tsNotice('所有必填项都不能为空！');
			}
			
			//修改课程
			$new['study']->update('study',array(
				'studyid'=>$studyid,
			),array(
				'userid'=>$userid,
				'studyname'=>$studyname,
				'studydesc'=>$studydesc,
				'price'=>$price,
				'cateid'=>$cateid,
				'cateid2'=>$cateid2,
			));



            //上传
            $arrUpload = tsUpload($_FILES['photo'],$studyid,'study',array('jpg','gif','png'));

            if($arrUpload){

                $new['study']->update('study',array(
                    'studyid'=>$studyid,
                ),array(
                    'path'=>$arrUpload['path'],
                    'photo'=>$arrUpload['url'],
                ));

                //删除缓存
                tsDimg($arrUpload['url'],'study','255','140',$arrUpload['path']);

            }

			
			tsNotice('基本信息修改成功!');
		
		}else{
		
			tsNotice('非法操作！');
		
		}
	
		break;
	

	//课时列表
	case "lesson":
		
		//用户是否登录
		$userid = aac('user')->isLogin();

		$studyid = intval($_GET['studyid']);

		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));
		
		if($strStudy['userid']==$userid || $TS_USER['isadmin']==1){
			
			//课程下课时列表
			$arrLesson = $new['study']->findAll('study_lesson',array(
				'studyid'=>$strStudy['studyid'],
			),'orderid asc');
			
			
			$title = '课时管理';
			include template('edit_lesson');
		}
		
		break;
	
	case "lessonadd":
		
		$userid = aac('user') -> isLogin();
		$studyid = intval($_GET['studyid']); 
		$strStudy = $new['study']->find('study',array(
		
			'studyid'=>$studyid,
		
		));
		
		if($strStudy['userid'] == $userid || $TS_USER['isadmin']==1){
		
			//第几课时
			$lessonNum = $new['study']->findCount('study_lesson',array(
				'studyid'=>$strStudy['studyid'],
			));
			
			$orderid = $lessonNum+1;
			
			//课程视频
			$arrVideo = $new['study']->findAll('study_video',array(
				'studyid'=>$strStudy['studyid'],
			));


			$title = '发布课时'; 
			// 包含模版
			include template("edit_lessonadd");
		
		}else{
		
			tsNotice('非法操作！');
		
		}
		
		break;
		
	case "lessonadddo":
		$userid = aac('user') -> isLogin();
        $studyid = intval($_POST['studyid']);
		$title = trim($_POST['title']);
		$content = tsCleanContent($_POST['content']);
		$orderid = intval($_POST['orderid']);

        $isprice = intval($_POST['isprice']);

		$videoid = intval($_POST['videoid']);
		
		if($title == '' || $content == '' || $orderid==0 || $studyid==0){
			tsNotice('课时发布输入不完整！');
		}
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));
	
		$lessonid = $new['study']->create('study_lesson',array(
			'studyid' => $studyid,
			'userid' => $userid,
			'orderid' => $orderid,
			'videoid' => $videoid,
			'title' => $title,
			'content' => $content,
            'isprice'=>$isprice,
			'addtime' => time(),
		));
		
		
		//统计总课时
		$count_lesson = $new['study']->findCount('study_lesson',array(
			'studyid'=>$studyid,
		));
		$new['study']->update('study',array(
			'studyid'=>$studyid,
		),array(
			'count_lesson'=>$count_lesson,
		));
		
		
		header("Location: " . tsUrl('study', 'edit', array('ts' =>'lesson','studyid'=>$studyid)));
	
		break;
		
	//
	case "lessonedit":
		$userid = aac('user')->isLogin();
		$lessonid = intval($_GET['lessonid']);
		$strLesson = $new['study']->find('study_lesson',array(
			'lessonid'=>$lessonid,
		));
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$strLesson['studyid'],
		));
		
		if($strStudy['userid']==$userid || $TS_USER['isadmin']==1){
		
			//课程视频
			$arrVideo = $new['study']->findAll('study_video',array(
				'studyid'=>$strStudy['studyid'],
			));
			
			$title = '编辑课时';
			include template('edit_lessonedit');
			
		}
		

		break;
		
	case "lessoneditdo":
	
        $userid = aac('user')->isLogin();
		
		$lessonid = intval($_POST['lessonid']);
		
		$title = trim($_POST['title']);
		$content = tsCleanContent($_POST['content']);
		
		$orderid = intval($_POST['orderid']);
		
		$videoid = intval($_POST['videoid']);

        $isprice = intval($_POST['isprice']);
		
		$strLesson= $new['study']->find('study_lesson',array(
			'lessonid'=>$lessonid,
		));
		
		$strStudy = $new['study']->find('study',array(
			'studyid'=>$strLesson['studyid'],
		));
	
		if($strStudy['userid']==$userid && $title && $content && $orderid  || $TS_USER['isadmin']==1 && $title && $content && $orderid){
	
			$new['study']->update('study_lesson',array(
				'lessonid'=>$lessonid
			),array(
				'orderid' => $orderid,
				'videoid' => $videoid,
				'title' => $title,
				'content' => $content,
                'isprice'=>$isprice,
			));
			
			header("Location: " . tsUrl('study', 'edit', array('ts' =>'lesson','studyid'=>$strStudy['studyid'])));
		
		}
	
		break;

    case "lessondel":

        $userid = aac('user')->isLogin();
        $lessonid = intval($_GET['lessonid']);
        $strLesson = $new['study']->find('study_lesson',array(
            'lessonid'=>$lessonid,
        ));

        $strStudy = $new['study']->find('study',array(
            'studyid'=>$strLesson['studyid'],
        ));

        if($strStudy['userid']==$userid || $TS_USER['isadmin']==1){

            $new['study']->delete('study_lesson',array(
                'lessonid'=>$lessonid,
            ));

            //统计总课时
            $count_lesson = $new['study']->findCount('study_lesson',array(
                'studyid'=>$studyid,
            ));
            $new['study']->update('study',array(
                'studyid'=>$studyid,
            ),array(
                'count_lesson'=>$count_lesson,
            ));

            header('Location: '.tsUrl('study','edit',array('ts'=>'lesson','studyid'=>$strStudy['studyid'])));

        }else{
            tsNotice('非法操作！');
        }

        break;
		
	//视频
	case "video":
	
		//用户是否登录
		$userid = aac('user')->isLogin();

		$studyid = intval($_GET['studyid']);

		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));
		
		if($strStudy['userid']==$userid || $TS_USER['isadmin']==1){
		
			//课程视频
			$arrVideo = $new['study']->findAll('study_video',array(
				'studyid'=>$strStudy['studyid'],
			));
			
			
			$title = '课程视频';
			include template('edit_video');
		
		}
	
		break;


	//删除视频
	case "videodel":

		//用户是否登录
		$userid = aac('user')->isLogin();

		$studyid = intval($_GET['studyid']);
		$videoid = intval($_GET['videoid']);

		$strStudy = $new['study']->find('study',array(
			'studyid'=>$studyid,
		));

		if($strStudy['userid']==$userid || $TS_USER['isadmin']==1){


			$strVideo = $new['study']->find('study_video',array(
				'videoid'=>$videoid,
				'studyid'=>$studyid,
			));


			unlink('uploadfile/study/video/'.$strVideo['video']);

			$new['study']->delete('study_video',array(
				'videoid'=>$videoid,
				'studyid'=>$studyid,
			));

			tsNotice('删除成功！');


		}

		break;
	
}